package indi.xliuovo.coursedesign.musicplayer;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import java.util.HashMap;
import java.util.Map;

public class ModifyActivity extends AppCompatActivity implements View.OnClickListener {

    private String tip = "正在努力开发，敬请期待...";
    private TextView userId;
    private EditText userNickName, userEmail, formerPassword, newPassword, confirmPassword;
    private ImageView modifyFace, modifyNickName, modifyEmail, bindWeibo, bindWechat, bindQQ, bindGoogle;
    private Button modifyInfo, modifyPassword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_modify);

        initViews();

        // 实现顶部返回按钮
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setHomeButtonEnabled(true);
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                this.finish(); // back button
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    // 初始化控件
    void initViews() {
        userId = (TextView) findViewById(R.id.userId);
        userNickName = (EditText) findViewById(R.id.nickName);
        userEmail = (EditText) findViewById(R.id.userEmail);
        formerPassword = (EditText) findViewById(R.id.formerPassword);
        newPassword = (EditText) findViewById(R.id.newPassword);
        confirmPassword = (EditText) findViewById(R.id.confirmPassword);
        modifyFace = (ImageView) findViewById(R.id.toModifyFace);
        modifyFace.setOnClickListener(this);
        modifyNickName = (ImageView) findViewById(R.id.toModifyNickName);
        modifyNickName.setOnClickListener(this);
        modifyEmail = (ImageView) findViewById(R.id.toModifyEmail);
        modifyEmail.setOnClickListener(this);
        bindWeibo = (ImageView) findViewById(R.id.toBindWeibo);
        bindWeibo.setOnClickListener(this);
        bindWechat = (ImageView) findViewById(R.id.toBindWeChat);
        bindWechat.setOnClickListener(this);
        bindQQ = (ImageView) findViewById(R.id.toBindQQ);
        bindWechat.setOnClickListener(this);
        bindGoogle = (ImageView) findViewById(R.id.toBindGoogle);
        bindGoogle.setOnClickListener(this);
        modifyInfo = (Button) findViewById(R.id.toModifyInfo);
        modifyInfo.setOnClickListener(this);
        modifyPassword = (Button) findViewById(R.id.toModifyPassword);
        modifyPassword.setOnClickListener(this);

        Intent intent = this.getIntent();
        Bundle bundle = intent.getExtras();
        userId.setText(bundle.getString("userId"));
        userNickName.setText(bundle.getString("userName"));
        if (bundle.getString("userEmail").equals("None"))
            userEmail.setHint("未设置邮箱");
        else
            userEmail.setText(bundle.getString("userEmail"));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.toModifyFace:
            case R.id.toBindWeibo:
            case R.id.toBindWeChat:
            case R.id.toBindQQ:
            case R.id.toBindGoogle:
                Toast.makeText(getApplicationContext(), tip, Toast.LENGTH_SHORT).show();
                break;
            case R.id.toModifyNickName:
                // 使可编辑
                userNickName.setFocusable(true);
                userNickName.setFocusableInTouchMode(true);
                Toast.makeText(getApplicationContext(), "昵称可编辑", Toast.LENGTH_SHORT).show();
                break;
            case R.id.toModifyEmail:
                // 使可编辑
                userEmail.setFocusable(true);
                userEmail.setFocusableInTouchMode(true);
                Toast.makeText(getApplicationContext(), "邮箱可编辑", Toast.LENGTH_SHORT).show();
                break;
            case R.id.toModifyInfo:
                modifyInfo();
                break;
            case R.id.toModifyPassword:
                modifyPassword();
                break;
        }
    }

    // 修改个人信息
    void modifyInfo() {
        // 传递数据
        Map<String, String> userInfo = new HashMap<String, String>();
        userInfo.put("userId", userId.getText().toString());
        if (TextUtils.isEmpty(userEmail.getText()))
            userInfo.put("userEmail", "None");
        else
            userInfo.put("userEmail", userEmail.getText().toString());
        if (TextUtils.isEmpty(userNickName.getText()))
            userInfo.put("userName", "user" + userId.getText().toString());
        else
            userInfo.put("userName", userNickName.getText().toString());
        MyThread myThread = new MyThread();
        myThread.toParams("user/updateUserInfo", userInfo); // 处理数据
        myThread.start();
        try {
            Thread.sleep(500); // 等待返回的结果
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        if (((Double) myThread.result.get("code")).intValue() == 400) {
            Toast.makeText(getApplicationContext(), String.valueOf(myThread.result.get("msg")), Toast.LENGTH_SHORT).show();
        } else if (((Double) myThread.result.get("code")).intValue() == 200) {
            Toast.makeText(getApplicationContext(), String.valueOf(myThread.result.get("msg")), Toast.LENGTH_SHORT).show();
        }
    }

    // 修改密码
    void modifyPassword() {
        // 传递数据
        if (checkPassword()) {
            Map<String, String> password = new HashMap<String, String>();
            password.put("userId", userId.getText().toString());
            password.put("oldPassword", formerPassword.getText().toString());
            password.put("newPassword", confirmPassword.getText().toString());
            MyThread myThread = new MyThread();
            myThread.toParams("user/updatePwd", password); // 处理数据
            myThread.start();
            try {
                Thread.sleep(500); // 等待返回的结果
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if (((Double) myThread.result.get("code")).intValue() == 400) {
                Toast.makeText(getApplicationContext(), String.valueOf(myThread.result.get("msg")), Toast.LENGTH_SHORT).show();
            } else if (((Double) myThread.result.get("code")).intValue() == 200) {
                Toast.makeText(getApplicationContext(), String.valueOf(myThread.result.get("msg")), Toast.LENGTH_SHORT).show();
                formerPassword.setText("");
                newPassword.setText("");
                confirmPassword.setText("");
            }
        }
    }

    // 检查密码格式
    boolean checkPassword() {
        if (TextUtils.isEmpty(formerPassword.getText()) || TextUtils.isEmpty(newPassword.getText())
                || TextUtils.isEmpty(confirmPassword.getText())) {
            Toast.makeText(getApplicationContext(), "密码不能为空", Toast.LENGTH_SHORT).show();
            return false;
        }
        if (!newPassword.getText().toString().equals(confirmPassword.getText().toString())) {
            Toast.makeText(getApplicationContext(), "两次密码不一致", Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }
}